#
import hmac, base64
import time

#
def token_generate(key, expire):
	""" 
		key('salt'): database stored
		expire('message') = expire + timeout
		token: = key + ':' + hmac.new(expire)
	"""
	# message
	ts_expire_byte = str(time.time() + expire).encode("utf-8")
	
	# generate sh1 string
	ts_sh1 = hmac.new(key.encode("utf-8"), ts_expire_byte, 'sha1').hexdigest()
	
	# token assemble
	token = ts_expire_byte.decode("utf-8") + ':' + ts_sh1
	
	# token encode to base64
	token_b64 = base64.urlsafe_b64encode(token.encode("utf-8"))
	
	# return  base64 decoded token
	return token_b64.decode("utf-8")


#

